package net.sangpil.classes.interactions
{
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import net.sangpil.classes.events.MouseDragEvent;
	
	[Event(name = "m_up", type = "net.sangpil.classes.events.MouseDragEvent")]
	[Event(name = "m_down", type = "net.sangpil.classes.events.MouseDragEvent")]
	[Event(name = "m_move", type = "net.sangpil.classes.events.MouseDragEvent")]
	
	public class MouseDrag extends EventDispatcher
	{
		private var sprite:Sprite;
		private var pt:Point;
		public function MouseDrag(sprite:Sprite)
		{
			super();
			this.sprite = sprite;
			this.pt = new Point();
			
			this.sprite.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
		}
		
		private function onDown(e:MouseEvent):void
		{
			this.dispatchEvent(new MouseDragEvent(new Point(this.sprite.x, this.sprite.y), MouseDragEvent.M_DOWN));
			this.sprite.startDrag();
			this.sprite.addEventListener(MouseEvent.MOUSE_UP, onUp);
			this.sprite.removeEventListener(MouseEvent.MOUSE_DOWN, onDown);
			
			this.sprite.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
		}
		
		private function onUp(e:MouseEvent):void
		{
			this.dispatchEvent(new MouseDragEvent(new Point(this.sprite.x, this.sprite.y), MouseDragEvent.M_UP));
			this.sprite.stopDrag();
			this.sprite.removeEventListener(MouseEvent.MOUSE_UP, onUp);
			this.sprite.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			
			this.sprite.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
		}
		
		private function onMove(e:MouseEvent):void
		{
			this.pt.x = this.sprite.x;
			this.pt.y = this.sprite.y;
			//trace(MouseDragEvent.M_MOVE);
			//var a:MouseDragEvent = new MouseDragEvent(this.pt, MouseDragEvent.M_MOVE);
			this.dispatchEvent(new MouseDragEvent(this.pt, MouseDragEvent.M_MOVE));
		}
		
	}
}